<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\company\model;

use think\Db;
use think\Model;

class Subscribe extends Model
{
    /**
     * @Desc 获取检车站名称
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getCompanyName($value)
    {
        return Db::name('company')->where('id', '=', $value)->value('company_name');
    }

    /**
     * @Desc 获取日期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getDate($value)
    {
        return Db::name('date')->where('id', '=', $value)->value('date_date');
    }

    /**
     * @Desc 获取星期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getWeek($value)
    {
        $week = '';
        $result = Db::name('date')->where('id', '=', $value)->value('date_week');
        switch ($result){
            case 1:
                $week = '星期一';
                break;
            case 2:
                $week = '星期二';
                break;
            case 3:
                $week = '星期三';
                break;
            case 4:
                $week = '星期四';
                break;
            case 5:
                $week = '星期五';
                break;
            case 6:
                $week = '星期六';
                break;
            case 0:
                $week = '星期日';
                break;
        }
        return $week;
    }

    /**
     * @Desc 获取日期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getStatus($value)
    {
        $status = '';
        $result = Db::name('date')->where('id', '=', $value)->value('status');
        switch ($result){
            case 0:
                $status = '工作日';
                break;
            case 1:
                $status = '法定节假日';
                break;
            case 2:
                $status = '休息日调休';
                break;
            case 3:
                $status = '休息日';
                break;
        }
        return $status;
    }

    // 获取分站检车项目列表
    public function getSubscribeList($pageNum, $numPerPage, $map)
    {
        $list = Db::view(['cd_subscribe' => 's'], ['id', 'date_id', 'company_id'])
                  ->view(['cd_date' => 'd'], ['date_date' => 'date', 'date_week' => 'week', 'status'], 'd.id=s.date_id')
                  ->where('company_id', '=', session('company.company_id'))
                  ->where($map)
                  ->order('date_date desc')
                  ->page($pageNum, $numPerPage)
                  ->group('s.date_id')
                  ->distinct(true)
                  ->select();
        foreach ($list as &$item){
            $item['company_name'] = $this->getCompanyName($item['company_id']);
            $item['date'] = $this->getDate($item['date_id']);
            $item['week'] = $this->getWeek($item['date_id']);
            $item['status'] = $this->getStatus($item['date_id']);
            unset($item['company_id']);
        }

        return $list;
    }

    // 获取分站检车项目总数
    public function getSubscribeCount($map)
    {
        $list = Db::view(['cd_subscribe' => 's'], ['date_id', 'company_id'])
                  ->view(['cd_date' => 'd'], ['date_date' => 'date', 'date_week' => 'week', 'status'], 'd.id=s.date_id')
                  ->where('company_id', '=', session('company.company_id'))
                  ->where($map)
                  ->order('date_date desc')
                  ->group('s.date_id')
                  ->distinct(true)
                  ->select();
        return count($list);
    }


    // 执行添加
    public function doAdd_subscribe($data)
    {
        // $data = [
        //     'date_date'     => '2020-06-16',
        //     'time_state_id' => [0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5', 5 => '6', 6 => '7', 7 => '8',],
        //     'num'           => [0 => '2', 1 => '3', 2 => '4', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '1',],
        // ];
        $insertData = [];
        $company_id = session('company.company_id');
        $date_id = db('date')->where('date_date', '=', $data['date_date'])->value('id');
        $subscribe = (new self)->where('date_id', '=', $date_id)->where('company_id', '=', $company_id)->find();
        if($subscribe){
            exception('该日期已经设置过');
        }
        foreach ($data['time_state_id'] as $key => $value) {
            $insertData[$key]['date_id'] = $date_id;
            $insertData[$key]['company_id'] = $company_id;
            $insertData[$key]['time_state_id'] = $value;
            $insertData[$key]['num'] = $data['num'][$key];
            if(45 == $company_id){
                $insertData[$key]['num_gas'] = $data['num_gas'][$key];
                $insertData[$key]['num_diesel'] = $data['num_diesel'][$key];
            }
        }
        $result = (new self)->saveAll($insertData);
        if (!$result) {
            exception('添加失败');
        }
    }

    /**
     * @desc 获取预约详情
     *
     * @param $data
     *
     * @return array
     */
    public function getDetail($data)
    {
        // $data = ['date' => '2021-03-01', 'date_id' => '274', 'company_name' => '平房区捷远检车站', '_' => '1614579657058',];
        $company_id = session('company.company_id');
        $time_state = Db::name('time_state')->order('time_state')->select();
        $list = [];
        $defaultNum = Db::name('base_subscribe_num')->value('num');
        foreach ($time_state as $key => $value) {
            $list[$key]['time_state'] = $value['time_state'];
            $list[$key]['time_state_id'] = $value['id'];
            $subscribe = Db::name('subscribe')->field('id, num, num_gas, num_diesel')->where('date_id', '=', $data['date_id'])->where('company_id', '=', $company_id)->where('time_state_id', '=', $value['id'])->find();
            $list[$key]['num'] = $subscribe['num'] ?? $defaultNum;
            $list[$key]['num_gas'] = $subscribe['num_gas'];
            $list[$key]['num_diesel'] = $subscribe['num_diesel'];
            if (45 != $company_id) {
                $list[$key]['subscribe_num'] = Db::name('order')->whereIn('zt', '0,1,2,3,10')->where('company_id', '=', $company_id)->where('subscribe_id', '=', $subscribe['id'])->count();
            } else {
                $subscribe_num = Db::name('order')->whereIn('zt', '0,1,2,3,10')->where('company_id', '=', $company_id)->where('subscribe_id', '=', $subscribe['id'])->where('vehicle_id', '=', 4)->count();
                $list[$key]['subscribe_num'] = $subscribe_num;
                $subscribe_num_gas = Db::name('order')->whereIn('zt', '0,1,2,3,10')->where('company_id', '=', $company_id)->where('subscribe_id', '=', $subscribe['id'])->where('vehicle_id', '=', 1)->count();
                $list[$key]['subscribe_num_gas'] = $subscribe_num_gas;
                $subscribe_num_diesel = Db::name('order')->whereIn('zt', '0,1,2,3,10')->where('company_id', '=', $company_id)->where('subscribe_id', '=', $subscribe['id'])->whereIn('vehicle_id', '2,3')->count();
                $list[$key]['subscribe_num_diesel'] = $subscribe_num_diesel;
            }
        }
        return $list;
    }

    // 执行修改
    public function doEdit_subscribe($data)
    {
        // $data = [
        //     'date_id'       => '275',
        //     'time_state_id' => [0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5', 5 => '6', 6 => '7', 7 => '8',],
        //     'num_gas'       => [0 => '0', 1 => '0', 2 => '0', 3 => '0', 4 => '0', 5 => '1', 6 => '4', 7 => '4',],
        //     'num_diesel'    => [0 => '0', 1 => '0', 2 => '0', 3 => '4', 4 => '5', 5 => '5', 6 => '5', 7 => '5',],
        //     'num'           => [0 => '0', 1 => '1', 2 => '3', 3 => '4', 4 => '4', 5 => '5', 6 => '5', 7 => '5',],
        // ];
        $company_id = session('company.company_id');
        foreach ($data['time_state_id'] as $key => $value) {
            $num = $data['num'][$key];
            $subscribe = (new self)->where('time_state_id', '=', $value)->where('date_id', '=', $data['date_id'])->where('company_id', '=', $company_id)->find();
            if ($subscribe) {
                $updateData = ['num' => $num];
                if(45 == $company_id){
                    $updateData['num_gas'] = $data['num_gas'][$key];
                    $updateData['num_diesel'] = $data['num_diesel'][$key];
                }
                self::update($updateData, ['id' => $subscribe['id']]);
            } else {
                $updateData = [
                    'date_id'       => $data['date_id'],
                    'company_id'    => $company_id,
                    'time_state_id' => $value,
                    'num'           => $num,
                ];
                if(45 == $company_id){
                    $updateData['num_gas'] = $data['num_gas'][$key];
                    $updateData['num_diesel'] = $data['num_diesel'][$key];
                }
                self::create($updateData);
            }
        }
    }

    //执行删除
    public function doDelete($userId)
    {
        $date_id = db('subscribe')->where('id', '=', $userId)->value('date_id');
        $result = db('subscribe')
            ->where('date_id', '=', $date_id)
            ->where('company_id', session('company.company_id'))
            ->delete();
        if (!$result) {
            exception('删除失败');
        }
    }
}